package versi.vitro.oaiserviceprovider.service.handle;

import junit.framework.Assert;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import versi.vitro.oaiserviceprovider.service.handle.api.HandleService;
import versi.vitro.oaiserviceprovider.service.handle.helper.HandleHelper;
import versi.vitro.oaiserviceprovider.service.handle.impl.HandleServiceImpl;

public class HandleServiceImplTest {

    private Mockery context;

    @BeforeMethod
    public void setUp() {
	context = new Mockery();
    }

    @Test
    public void testConfigureANDSHandleService() {

	final String url = "dummy url value";
	final String AppId = "dummy AppId value";
	final String identifier = "dummy identifier value";
	final String authDomain = "dummy authDomain value";

	final HandleHelper handleHelper = context.mock(HandleHelper.class);
	context.checking(new Expectations() {
	    {
		oneOf(handleHelper).setAndServiceURL(url);
		oneOf(handleHelper).setServiceAppId(AppId);
		oneOf(handleHelper).setAuthDomain(authDomain);
		oneOf(handleHelper).setIdentifier(identifier);
	    }
	});

	HandleService hs = new HandleServiceImpl(handleHelper);

	hs.configureANDSHandleService(url, AppId, identifier, authDomain);
	context.assertIsSatisfied();
    }

    @Test
    public void testPidCreateHandle() {

	final String resourceType = "dummy resourceType value";
	final String resourceValue = "dummyResourceValue";

	final HandleHelper handleHelper = context.mock(HandleHelper.class);
	context.checking(new Expectations() {
	    {
		oneOf(handleHelper).pidCreateHandle(resourceType, resourceValue);
		will(returnValue("dummy handle value"));

	    }
	});

	HandleService hs = new HandleServiceImpl(handleHelper);

	String handle = hs.pidCreateHandle(resourceType, resourceValue);
	assert handle.equals("dummy handle value");
	context.assertIsSatisfied();

    }

    @Test
    public void testPidCreateHandleWithIndex() {

	final String resourceType = "dummy resourceType value";
	final String resourceValue = "dummyResourceValue";
	final int index = 1;

	final HandleHelper handleHelper = context.mock(HandleHelper.class);
	context.checking(new Expectations() {
	    {
		oneOf(handleHelper).pidCreateHandle(resourceType, resourceValue, index);
		will(returnValue("dummy handle value"));

	    }
	});

	HandleService hs = new HandleServiceImpl(handleHelper);

	String handle = hs.pidCreateHandle(resourceType, resourceValue, index);
	assert handle.equals("dummy handle value");
	context.assertIsSatisfied();

    }

    @Test
    public void testPidDeleteHandleValue() {

	final String handle = "10000.0/0001";
	final int index = 1;

	// pidDeleteHandleValue

	final HandleHelper handleHelper = context.mock(HandleHelper.class);
	context.checking(new Expectations() {
	    {
		oneOf(handleHelper).pidDeleteValueByIndex(handle, index);
		will(returnValue(true));

	    }
	});

	HandleService hs = new HandleServiceImpl(handleHelper);

	boolean success = hs.pidDeleteHandleValue(handle, index);

	Assert.assertEquals(true, success);
	context.assertIsSatisfied();

    }

    @Test
    public void testPidModifyHandleValue() {

	final String handle = "10000.0/0001";
	final int index = 1;
	final String resourceValue = "dummyResourceValue";

	final HandleHelper handleHelper = context.mock(HandleHelper.class);
	context.checking(new Expectations() {
	    {
		oneOf(handleHelper).pidModifyHandle(handle, index, resourceValue);
		will(returnValue(true));

	    }
	});

	HandleService hs = new HandleServiceImpl(handleHelper);

	boolean success = hs.pidModifyHandleValue(handle, index, resourceValue);

	Assert.assertEquals(true, success);
	context.assertIsSatisfied();

    }

    @Test
    public void pidAddHandleValue() {

	final String handle = "10000.0/0001";
	final String resourceType = "DESC";
	final String resourceValue = "dummyResourceValue";

	final HandleHelper handleHelper = context.mock(HandleHelper.class);
	context.checking(new Expectations() {
	    {
		oneOf(handleHelper).pidAddValue(handle, resourceType, resourceValue);
		will(returnValue("dummy handle value"));

	    }
	});

	HandleService hs = new HandleServiceImpl(handleHelper);

	String handleResult = hs.pidAddHandleValue(handle, resourceType, resourceValue);

	assert handleResult.equals("dummy handle value");
	context.assertIsSatisfied();

    }

    @Test
    public void pidAddHandleValueWithIndex() {

	final String handle = "10000.0/0001";
	final String resourceType = "DESC";
	final String resourceValue = "dummyResourceValue";
	final int index = 1;

	final HandleHelper handleHelper = context.mock(HandleHelper.class);
	context.checking(new Expectations() {
	    {
		oneOf(handleHelper).pidAddValue(handle, resourceType, resourceValue, index);
		will(returnValue("dummy handle value"));

	    }
	});

	HandleService hs = new HandleServiceImpl(handleHelper);

	String handleResult = hs.pidAddHandleValue(handle, resourceType, resourceValue, index);

	assert handleResult.equals("dummy handle value");
	context.assertIsSatisfied();

    }

}
